//
//  DetailTableView.m
//  StationManager
//
//  Created by 张佳强 on 17/3/7.
//  Copyright © 2017年 Helong. All rights reserved.
//

#import "DetailTableView.h"
#import "DetailCell.h"
#import "StationInfoModel.h"
#import <BaiduMapAPI_Utils/BMKUtilsComponent.h>

@implementation DetailTableView

- (instancetype)initWithFrame:(CGRect)frame style:(UITableViewStyle)style {
    
    self = [super initWithFrame:frame style:style];
    if (self) {
        
        self.delegate = self;
        self.dataSource = self;
        self.separatorStyle = UITableViewCellSelectionStyleNone;
        
    }
    return self;
}

#pragma TableView-delegate

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return 1;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    
    return self.dataArray.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    
    return 10;
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    
    return 0.1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    static NSString * iden = @"damage";
    DetailCell * cell = [self dequeueReusableCellWithIdentifier:iden];
    if (cell == nil) {
        cell = [[DetailCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:iden];
    }
    self.stationModel = _dataArray[indexPath.section];
    cell.stationName.text = [NSString stringWithFormat:@"站名：%@",self.stationModel.stationName];
    cell.stationNumber.text = [NSString stringWithFormat:@"站号：%@",self.stationModel.stationNum];
    cell.location.text = [NSString stringWithFormat:@"导航位置：%@",self.stationModel.navigationPosition];
    
    cell.address.text = [NSString stringWithFormat:@"位置：%@",self.stationModel.position];
    BMKMapPoint point1 = BMKMapPointForCoordinate(CLLocationCoordinate2DMake(self.userLatitude, self.userLongitude));
    BMKMapPoint point2 = BMKMapPointForCoordinate(CLLocationCoordinate2DMake([self.stationModel.latitude floatValue], [self.stationModel.longitude floatValue]));
    CLLocationDistance distance = BMKMetersBetweenMapPoints(point1,point2);
    cell.distance.text = [NSString stringWithFormat:@"%.2f公里",distance/1000];
    return cell;
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    return 90;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    self.stationModel = _dataArray[indexPath.section];
    NSArray * pointArray = @[self.stationModel.latitude,self.stationModel.longitude];
    [[NSNotificationCenter defaultCenter] postNotificationName:@"Located" object:pointArray];
    [[self getController].navigationController popViewControllerAnimated:YES];
}
- (UIViewController *)getController{
    for (UIView* next = [self superview]; next; next = next.superview) {
        UIResponder* nextResponder = [next nextResponder];
        if ([nextResponder isKindOfClass:[UIViewController class]]) {
            UIViewController *vc =  (UIViewController*)nextResponder;
            return vc;
        }
    }
    return nil;
}
@end
